package com.huanwei.login_regist;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.huanwei.cc.R;
import com.huanwei.cc.activity.SetIp_Activity;
import com.huanwei.cc.db.Z_Login_UserInfoDao;
import com.huanwei.http.API;
import com.huanwei.http.Okhttp;
import com.huanwei.cc.newbean.Login;
import com.huanwei.cc.newbean.LoginT;
import com.huanwei.cc.utils.JSONUtils;
import com.huanwei.cc.utils.SpfUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 登录页面，
 */
public class LoginActivity extends AppCompatActivity {

    @BindView(R.id.login_phone)
    EditText loginPhone;
    @BindView(R.id.login_mm)
    EditText loginMm;
    @BindView(R.id.but_login)
    Button butLogin;
    @BindView(R.id.but_regist)
    Button butRegist;
    Z_Login_UserInfoDao z_userInfoDao;
    @BindView(R.id.img_back)
    RelativeLayout imgBack;


    String type="1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        z_userInfoDao = new Z_Login_UserInfoDao(this);




    }

    @OnClick({R.id.img_back, R.id.but_login, R.id.but_regist, R.id.but_setip})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.but_setip:
                startActivity(new Intent(this, SetIp_Activity.class));
                break;
            case R.id.but_login:
                if (TextUtils.isEmpty(loginPhone.getText().toString())) {
                    Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(loginMm.getText().toString())) {
                    Toast.makeText(this, "请密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                API.login(loginPhone.getText().toString(), loginMm.getText().toString(), type, new Okhttp.Objectcallback() {
                    @Override
                    public void onsuccess(String st) {
                        Log.e("111", "xx" + st);
                        if (type.equals("1")) {
                            Login login = JSONUtils.parseJSON(st, Login.class);
                            if (login.isIssuccess() == true) {
                                Toast.makeText(LoginActivity.this, "登录成功！", Toast.LENGTH_SHORT).show();
                                SpfUtils.put(LoginActivity.this, "stlogin", st);
                                SpfUtils.put(LoginActivity.this, "islogin", true);
                                SpfUtils.put(LoginActivity.this, "type", "1");
                                startActivity(new Intent(LoginActivity.this, Student_Activity.class));

                                finish();
                            } else {
                                Toast.makeText(LoginActivity.this, login.getMsg(), Toast.LENGTH_SHORT).show();

                            }

                        } else {

                        }


                    }

                    @Override
                    public void onFalia(int code, String errst) {
                        Toast.makeText(LoginActivity.this, ""+errst, Toast.LENGTH_SHORT).show();
                    }
                });

//                if (z_userInfoDao.shifoucunzai(loginPhone.getText().toString())) {
//                    if (!z_userInfoDao.getpassword(loginPhone.getText().toString()).equals(loginMm.getText().toString())) {
//                        Toast.makeText(this, "密码错误！", Toast.LENGTH_SHORT).show();
//                    } else {
//                        Toast.makeText(this, "登录成功！", Toast.LENGTH_SHORT).show();
//                        startActivity(new Intent(this, MainActivity.class));
//                        SpfUtils.put(this, "islogin", true);
//                        SpfUtils.put(this, "name", loginPhone.getText().toString());
//                        finish();
//                    }
//
//                } else {
//                    Toast.makeText(this, "账号不存在！", Toast.LENGTH_SHORT).show();
//                }
                break;
            case R.id.but_regist:
                startActivity(new Intent(this, RegistActivity.class));
                break;



        }
    }
}
